package com.rankeiot.platform;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.core.filters.GlobalOpenApiMethodFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnClass(name="io.swagger.v3.oas.models.OpenAPI")
//@ConditionalOnClass(name="org.springdoc.core.customizers.OpenApiCustomizer")
@Configuration
public class DocConfig {
    @Bean
    GlobalOpenApiMethodFilter globalOpenApiMethodFilter(){
        return method -> method.getAnnotation(Operation.class)!=null;
    }
    /**
     * 添加全局的请求头参数
     */
    @Bean
    public OpenApiCustomizer customerGlobalHeaderOpenApiCustomizer() {
        return api -> {
            api.components(new Components()
                    .addSecuritySchemes("token",
                            new SecurityScheme()
                                    .name("token")
                                    .type(SecurityScheme.Type.HTTP)
                                    .in(SecurityScheme.In.HEADER)
                                    .description("TOKEN 认证")
                    )
            );
        };
    }
    @Bean
    public OperationCustomizer operationCustomizer() {
        return (operation, handlerMethod) -> {
            operation.addSecurityItem(new SecurityRequirement().addList("token"));
            return operation;
        };
    }
}
